package com.team1.system.libraryident.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.team1.system.libraryuser.domain.LibraryUser;
import com.team1.system.libraryuser.service.ILibraryUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.team1.common.annotation.Log;
import com.team1.common.core.controller.BaseController;
import com.team1.common.core.domain.AjaxResult;
import com.team1.common.enums.BusinessType;
import com.team1.system.libraryident.domain.LibraryIdent;
import com.team1.system.libraryident.service.ILibraryIdentService;
import com.team1.common.utils.poi.ExcelUtil;
import com.team1.common.core.page.TableDataInfo;

/**
 * 认证管理Controller
 *
 * @author hyy
 * @date 2023-04-09
 */
@RestController
@RequestMapping("/librarysystem/libraryident")
public class LibraryIdentController extends BaseController
{
    @Autowired
    private ILibraryIdentService libraryIdentService;

    @Autowired
    private ILibraryUserService libraryUserService;

    /**
     * 查询认证管理列表
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryident:list')")
    @GetMapping("/list")
    public TableDataInfo list(LibraryIdent libraryIdent)
    {
        startPage();
        List<LibraryIdent> list = libraryIdentService.selectLibraryIdentList(libraryIdent);
        return getDataTable(list);
    }

    /**
     * 导出认证管理列表
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryident:export')")
    @Log(title = "认证管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LibraryIdent libraryIdent)
    {
        List<LibraryIdent> list = libraryIdentService.selectLibraryIdentList(libraryIdent);
        ExcelUtil<LibraryIdent> util = new ExcelUtil<LibraryIdent>(LibraryIdent.class);
        util.exportExcel(response, list, "认证管理数据");
    }

    /**
     * 获取认证管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryident:query')")
    @GetMapping(value = "/{identId}")
    public AjaxResult getInfo(@PathVariable("identId") Long identId)
    {
        return success(libraryIdentService.selectLibraryIdentByIdentId(identId));
    }

    /**
     * 新增认证管理
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryident:add')")
    @Log(title = "认证管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LibraryIdent libraryIdent)
    {
        LibraryUser user=libraryUserService.selectLibraryUserByUserId(getUserId());
        if (user.getUserIdent().equals("1")){
            return error("已认证用户不能重复认证");
        }
        if (user.getUserIdent().equals("9")){
            return error("正在认证中…");
        }
        libraryIdent.setIdentIs("0");
        libraryIdent.setIdentUserid(getUserId());
        libraryIdent.setIdentBefore(user.getUserIdent());
        user.setUserIdent("9");
        libraryUserService.updateLibraryUser(user);
        return toAjax(libraryIdentService.insertLibraryIdent(libraryIdent));
    }

    /**
     * 修改认证管理
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryident:edit')")
    @Log(title = "认证管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LibraryIdent libraryIdent)
    {
        if (libraryIdent.getIdentIs().equals("1")){
            return error("已被操作的认证");
        }
        LibraryUser user=libraryUserService.selectLibraryUserByUserId(libraryIdent.getIdentUserid());
        user.setUserIdent("1");
        libraryIdent.setIdentIs("1");
        libraryIdent.setIdentAfter("1");
        libraryIdent.setIdentAdminid(getUserId());
        libraryIdent.setIdentTime(new Date());
        libraryUserService.updateLibraryUser(user);
        return toAjax(libraryIdentService.updateLibraryIdent(libraryIdent));
    }

    /**
     * 修改认证管理
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryident:edit')")
    @Log(title = "认证管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit2")
    public AjaxResult edit2(@RequestBody LibraryIdent libraryIdent)
    {
        if (libraryIdent.getIdentIs().equals("1")){
            return error("已被操作的认证");
        }
        LibraryUser user=libraryUserService.selectLibraryUserByUserId(libraryIdent.getIdentUserid());
        user.setUserIdent("2");
        libraryIdent.setIdentIs("1");
        libraryIdent.setIdentAfter("2");
        libraryIdent.setIdentAdminid(getUserId());
        libraryIdent.setIdentTime(new Date());
        libraryUserService.updateLibraryUser(user);
        return toAjax(libraryIdentService.updateLibraryIdent(libraryIdent));
    }

    /**
     * 删除认证管理
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryident:remove')")
    @Log(title = "认证管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{identIds}")
    public AjaxResult remove(@PathVariable Long[] identIds)
    {
        return toAjax(libraryIdentService.deleteLibraryIdentByIdentIds(identIds));
    }
}
